class orderEdgePoints{
    /**
     * 按照斜率排序每个点
     */
    constructor() {}
    static getInstance(){
        if(!orderEdgePoints.instance) {
            orderEdgePoints.instance = new orderEdgePoints();
        }
        return orderEdgePoints.instance;
    }
    /**
     * 获取最大值或者最小值
     * @param data  数组
     * @param flied  字段
     * @param type   类型
     * @returns {*}
     */
    getMaxorMinValue(data,flied='lat',type='min') {
        let ret = data[0][flied];
        for (let i = 1 ;i<data.length; i++) {
            if(type=='max'){
                if(data[i][flied] > ret ){
                    ret = data[i][flied];
                }
            }
            if(type=='min'){
                if(data[i][flied] < ret ){
                    ret = data[i][flied];
                }
            }
        }
        return ret;
    }

    /**
     * 获取中心点
     * @param arr
     */
    getCenterPoint (arr){
        this.minLat = this.getMaxorMinValue(arr,'lat');
        this.maxLat = this.getMaxorMinValue(arr,'lat','max');
        this.minLng = this.getMaxorMinValue(arr,'lng');
        this.maxLng = this.getMaxorMinValue(arr,'lng','max');
        this.centerLat = ( this.minLat + this.maxLat) / 2;
        this.centerLng = ( this.minLng + this.maxLng) / 2;

    }

    /**
     * 按照斜率排序每个点
     * @param data
     * @returns {*}
     */
    orderPoints(data) {
        this.getCenterPoint(data);
        for (let i = 0; i < data.length; i++) {

               let k = ( data[i].lng  - this.centerLng) / ( data[i].lat - this.centerLat );
               data[i].k = k;
        }
        for(let i=0;i<data.length;i++){
            for(let z=0;z< data.length -i -1 ;z++){
                  if( data[z].k > data[z+1].k ) {
                        let t = data[z+1];
                        data[z+1] = data[z];
                        data[z] = t;
                  }
            }
        }

        return data;

    }



}